/*
	Copyright (c) [2019] [一只程序缘 jiezhuo]
	[https://gitee.com/jiezhuonew/oledlib] is licensed under the Mulan PSL v1.
	You can use this software according to the terms and conditions of the Mulan PSL v1.
	You may obtain a copy of Mulan PSL v1 at:
		http://license.coscl.org.cn/MulanPSL
	THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
	PURPOSE.
	See the Mulan PSL v1 for more details.
	
	此c文件用于配置oled
	例如初始化oled引脚 刷新oled
	注意!!! 
	如需演示动画效果 OledTimeMsFunc()需放入1ms中断等中1ms调用一次为库提供时间基准 原作是放入滴答定时器中
	
//二次移植：
	
YQY不专业的注释：
该文件已经全面适配中景园0.96寸OLED 12864屏幕！
该文件为精简版，适用于oledlib库


目前设计为全局刷新 绘图函数等只需要向OLED_GRAM[8][128]内画点即可


移植说明：

	零：
		去上方魔术棒工具中打开Use MicroLIB选项 （因为使用了stdio.h头文件，尽量减小程序体积）
		
	壹：传入iic：
		//oled_set_i2c_handle(hi2c1);			//到i2c.c中的MX_I2C1_Init(void)更新该句柄
		
	贰：去main函数中调用init_oled();通过iic发送初始化参数，完成SSD1306(12864屏幕驱动)初始化
	
	叁：OledTimeMsFunc()放入1ms中断中提供时基



原理说明：

	定义模拟显存ScreenBuffer[8][128]，一次刷新整个显存的数据进入屏幕，提高屏幕刷新速率
*/

#include "oled_config.h"
#include "oled_driver.h"
#include "delay.h"

#include "draw_api.h"				//用于下方的YQY_TEST测试函数


//模拟显存
extern unsigned char ScreenBuffer[SCREEN_PAGE_NUM][SCREEN_COLUMN];
extern unsigned char TempBuffer[SCREEN_PAGE_NUM][SCREEN_COLUMN];

//时间基准
unsigned int OledTimeMs=0;

//初始化图形库，请将硬件初始化信息放入此中 所有已经通过CubeMX初始化完成，无需再次配置  YQY-2022.1.4
void DriverInit(void)
{
	#if (TRANSFER_METHOD == HW_IIC) || (TRANSFER_METHOD == SW_IIC)
	//已经通过CubeMX初始化完成，无需再次配置  YQY-2022.1.4
	//I2C_Configuration();
	#elif (TRANSFER_METHOD == HW_SPI) || (TRANSFER_METHOD == SW_SPI)
	SPI_Configuration();	//初始化接口
	#endif
	//已经通过oled_init();实现，无需再次配置  YQY-2022.1.4
	//OLED_Init();			//初始化配置oled
}

//将ScreenBuffer屏幕缓存的内容显示到屏幕上
void UpdateScreenBuffer(void)
{
	//OLED_FILL(ScreenBuffer[0]);
	oled_send_data_arr((unsigned char *)ScreenBuffer, 128*8);//发送数据-数组
}
//将TempBuffer临时缓存的内容显示到屏幕上
void UpdateTempBuffer(void)
{
	//OLED_FILL(TempBuffer[0]);
	oled_send_data_arr((unsigned char *)TempBuffer, 128*8);//发送数据-数组
}

//////////////////////////////////////////////////////////
//请将此函数放入1ms中断里，为图形提供时基
//系统时间基准主要用于FrameRateUpdateScreen()中固定帧率刷新屏幕
void OledTimeMsFunc(void)
{
	if(OledTimeMs != 0x00)
	{ 
		OledTimeMs--;
	}
}
//图形库普通的延时函数 需要用户自己配置 这里在delay.c中调用
void DelayMs(uint16_t ms)
{
	//delay_ms(ms);
	HAL_Delay(ms);
}


I2C_HandleTypeDef  var_i2c;							//定义iic控制句柄


void oled_set_i2c_handle(I2C_HandleTypeDef ref_value)
{
	var_i2c = ref_value;
}

void oled_send_cmd(unsigned char I2C_Command)//发送命令
{
    uint8_t *pData;
    pData = &I2C_Command;
	HAL_I2C_Mem_Write(&var_i2c, OLED_DEV_ADDR, 0x00,I2C_MEMADD_SIZE_8BIT,pData,1,100);
	//HAL_I2C_Mem_Write_DMA(&var_i2c, OLED_DEV_ADDR, 0x00, I2C_MEMADD_SIZE_8BIT, pData, 1);
}


	

void oled_send_data(unsigned char I2C_Data)//发送数据
{
	HAL_I2C_Mem_Write(&var_i2c, OLED_DEV_ADDR, 0x40,I2C_MEMADD_SIZE_8BIT, &I2C_Data,1,100);
}

void oled_send_data_arr(unsigned char *I2C_Data_Arr, uint16_t count)//发送数据-数组  有用 别删除！
{
	HAL_I2C_Mem_Write(&var_i2c, OLED_DEV_ADDR, 0x40,I2C_MEMADD_SIZE_8BIT, I2C_Data_Arr, count, 100);
	//DMA传输有BUG 再测！！！！！！！
	//HAL_I2C_Mem_Write_DMA(&var_i2c, OLED_DEV_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, I2C_Data_Arr, 128*64);
}

	
void init_oled( void){		//oled初始化函数
	oled_send_cmd(0xAE); 
	oled_send_cmd(0x00);
	oled_send_cmd(0x10);
	oled_send_cmd(0x40);
	oled_send_cmd(0x81);
	oled_send_cmd(0xCF);
	oled_send_cmd(0xA1);
	oled_send_cmd(0xC8);
	oled_send_cmd(0xA6);
	oled_send_cmd(0xA8);
	oled_send_cmd(0x3F);
	oled_send_cmd(0xD3);
	oled_send_cmd(0x00);
	oled_send_cmd(0xD5);
	oled_send_cmd(0x80);
	oled_send_cmd(0xD9);
	oled_send_cmd(0xF1); 
	oled_send_cmd(0xDA); 
	oled_send_cmd(0x12); 
	oled_send_cmd(0xDB); 
	oled_send_cmd(0x30); 
	oled_send_cmd(0x20);
	oled_send_cmd(0x00);		//修改成横向刷新
	oled_send_cmd(0x8D);
	oled_send_cmd(0x14);
	
	oled_send_cmd(0xAF); //开启显示
	
	oled_send_cmd(0xb0); 	 //设置行起始地址
	oled_send_cmd(0x00);   //设置低列起始地址
	oled_send_cmd(0x10);   //设置高列起始地址
	
}



void YQY_oled_test()
{
	ClearScreen();
	DrawBitmap(0,0,Panda,128,64);
	UpdateScreen();
	DelayMs(1000);
	ClearScreen();
	UpdateScreen();
}
